package com.product.android.ui.imageHolder;

import java.io.FileNotFoundException;

import uk.co.senab.photoview.PhotoView;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.common.android.ui.gif.GifImageView;
import com.common.android.utils.FileUtils;
import com.common.android.utils.ImageUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.EventBusException;

public abstract class Picture {
	public PhotoView ivPicture;//用于显示普通图片
	public GifImageView gifPicture;//用于显示GIF图片
	public RelativeLayout rlProgress;//圆圈进度条布局
	public CircleProgress cpProgress;//圆圈进度条
	public Button leftBtn;//左边按钮
	public Button rightBtn;//右边按钮
	public int pos; //当前图片的位置，如果单张图片，直接设置-1，如果多张图片设置相应的position。
	public boolean isShow = true;//是否显示进度条
	public String url;//图片URL
	public String thumbnail;//缩略图URL
	public String absPath = "";// 图片下载完成后的绝对路径。只有本地存在的时候才非空
	public boolean isLocalUrl = false;//图片是否是本地的
	public DisplayImageOptions displayImageOpt;//显示图片需要的一些配置，比如图片所在的文件夹目录等
	private ImageLoader mImageLoader = ImageLoader.getInstance();//加载图片的控件
	/**
	 * 
	 * @n<b>函数名称</b>     :isGif
	 * @brief
	 * @see
	 * @since    Ver 1.1
	 * @param  @return
	 * @return boolean
	 * @<b>作者</b>          :  chenqian
	 * @<b>创建时间</b>      :  2014-3-24下午2:46:24
	 */
	public abstract boolean isGif();
	public DownloadListener progressListener = new DownloadListener() {//侦听下载进度条

		@Override
		public void success(String url) {
			EventBus.getDefault().post(new DownloadProgress(100, url));
		}

		@Override
		public void fail(String url) {
			EventBus.getDefault().post(new DownloadProgress(-1, url));
		}

		@Override
		public void showProgress(long totalSize, long downloadSize, String url) {
			if(0 == totalSize){
				return;
			}
			long percentage = downloadSize * 100 / totalSize;
			EventBus.getDefault().post(
					new DownloadProgress((int) percentage, url));
		}

	};
	
	public SimpleImageLoadingListener loadingListener = new SimpleImageLoadingListener(){//侦听图片下载

		@Override
		public void onLoadingComplete(String imageUri, View view,
				Bitmap loadedImage) {
			progressListener.success(imageUri);
			unRegisterEvent();
			absPath = mImageLoader.getDiscCacheFileAbsPath(displayImageOpt,url);
			
			if(isGif() || ImageUtils.isGifFile(absPath)){
				//如果是GIF图片，通过imageloader下载完成后，需要传递本地保存路径到gif的显示控件
				ivPicture.setVisibility(View.GONE);
				if(gifPicture != null){
					gifPicture.setVisibility(View.VISIBLE);				
					try {
						gifPicture.setGifImage(absPath);
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					}
				}
			}else{
				ivPicture.setVisibility(View.VISIBLE);
				ivPicture.setImageBitmap(loadedImage);
				if(gifPicture != null){
					gifPicture.setVisibility(View.GONE);
				}
			}
			
		}
		
		@Override
		public void onLoadingCancelled(String imageUri, View view) {
			progressListener.fail(imageUri);
			unRegisterEvent();
		}

		@Override
		public void onLoadingFailed(String imageUri, View view,
				FailReason failReason) {
			progressListener.fail(imageUri);
			unRegisterEvent();
		}

		@Override
		public void onLoadingProgress(long totalSize, long downloadSize) {
			progressListener.showProgress(totalSize,
					downloadSize, url);
		}

		@Override
		public void onLoadingStarted(String imageUri, View view) {
			registerEvent();
			progressListener.showProgress(100, 0, imageUri);
		}
		
	};

	public void registerEvent() {
		try {
			EventBus.getDefault().register(this);	
		} catch (EventBusException e) {
			
		}
	}

	public void unRegisterEvent() {
		EventBus.getDefault().unregister(this);
	}
	
	public void onEventMainThread(Object obj) {
		if (obj instanceof DownloadProgress) {
			DownloadProgress dp = (DownloadProgress) obj;
			if (dp.mUrl.equals(url)) {
				if (dp.mPercentage == -1) {
					rlProgress.setVisibility(View.GONE);
				} else if (dp.mPercentage == 0) {
					rlProgress.setVisibility(View.VISIBLE);
					cpProgress.setMainProgress(dp.mPercentage);
				} else if (dp.mPercentage >= 100) {
					rlProgress.setVisibility(View.GONE);

				} else {
					if (isShow) {
						rlProgress.setVisibility(View.VISIBLE);
					} else {
						rlProgress.setVisibility(View.GONE);
					}
					cpProgress.setMainProgress(dp.mPercentage);
				}
			}
		}
	}
	
	public void showImage(String thumbnail, String url, DisplayImageOptions displayImageOpt){//专门用于显示图片，不论是GIF图片还是普通图片
		this.thumbnail = thumbnail;
		this.url = url;
		this.displayImageOpt = displayImageOpt;
		if(!isGif()){
			if(isLocalUrl){
				url= FileUtils.LOCAL_FILE_PREFIX + url;//如果是本地的普通图，必须加上"file://"字段才能被imageloader加载
				thumbnail= FileUtils.LOCAL_FILE_PREFIX + thumbnail;//如果是本地的普通图，必须加上"file://"字段才能被imageloader加载
			}
			ivPicture.setVisibility(View.VISIBLE);
			if(gifPicture != null){
				gifPicture.setVisibility(View.GONE);
			}
			mImageLoader.displayImage(thumbnail, url,ivPicture,displayImageOpt,loadingListener);
		}else{
			ivPicture.setVisibility(View.GONE);
			if(gifPicture != null)
				gifPicture.setVisibility(View.VISIBLE);
			if(isLocalUrl){
				try {
					if (ImageUtils.isGifFile(url)){
						//确实是GIF图片
						if(gifPicture != null)  gifPicture.setGifImage(url);	
					}else{
						//扩展名是GIF,实际上不是GIF
						url= FileUtils.LOCAL_FILE_PREFIX + url;//如果是本地的普通图，必须加上"file://"字段才能被imageloader加载
						thumbnail= FileUtils.LOCAL_FILE_PREFIX + thumbnail;//如果是本地的普通图，必须加上"file://"字段才能被imageloader加载
						mImageLoader.displayImage(thumbnail, url,ivPicture,displayImageOpt,loadingListener);
					}
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
			}else{
				Bitmap bmp=mImageLoader.findCachedBitmapForImageUri(thumbnail);
				if (bmp!=null && gifPicture != null){
					gifPicture.setImageBitmap(bmp);
				}
				ImageLoader.getInstance().loadImage(gifPicture,url, displayImageOpt, loadingListener);
			}
			
		}
	}

	public static class DownloadProgress {
		public int mPercentage;
		public String mUrl;

		public DownloadProgress(int percentage, String url) {
			mPercentage = percentage;
			mUrl = url;
		}
	}
	
	/**
	 * @函数名称     :isDownloading
	 * @brief 
	 * @see
	 * @since    Ver 1.1
	 * @return
	 * @作者          :  Administrator
	 * @创建时间      :  2014-6-5上午11:24:25      
	*/
	public boolean isDownloading(){
		return rlProgress.getVisibility()==View.VISIBLE;
	}
}
